<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>canvas直线</title>
    <style>
        /*不能在里面设置宽高*/
        canvas{

            border: 1px solid black;
            transition: 2s linear;
        }
    </style>
</head>
<body>
<button id="bt">点击绘画</button>
<hr>

</body>


<script>
    const canvas = document.createElement('canvas');
    document.body.append(canvas)
    canvas.width = 600
    canvas.height = 400
    const ctx = canvas.getContext('2d')

    const geneateCanvas = () => {
        //1.moveTo方法画笔移动到直线起点
        //2.lineTo方法画笔移动到直线终点
        //3.使用stroke方法画直接
        //4.画折线描点要一次描点,然后stroke()连接点，多次stroke()线会变粗的,stroke会把所有点连接一次
        ctx.moveTo(10, 10);
        ctx.lineTo(150, 180);
        ctx.lineTo(200, 70);
        ctx.lineTo(300, 330);
        ctx.stroke()
    }

    const button = document.getElementById('bt');
    button.addEventListener('click',geneateCanvas)



</script>
</html>